package org.crud4j.core;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

/**
 * Bean definition
 */
public class CrudBeanDef implements Serializable {

	/**
	 * Id type
	 */
	private Class idType;

	/**
	 * label
	 */
	private String label;

	/**
	 * Instance label expression
	 */
	private String instanceLabelExpression;

	/**
	 * Type of the given bean
	 */
	private Class type;

	/**
	 * 
	 * Field definitions of this bean
	 */
	private Map<String, CrudFieldDef> fieldDefsMap;

	/**
	 * Flag that indicates whether the fiels must be sorted
	 */
	private boolean fieldsSorted;

	/**
	 * Flag that indicates whether the bean creation is disabled
	 */
	private boolean newDisabled;

	/**
	 * Flag that indicates whether the bean edition is disabled
	 */
	private boolean editDisabled;

	/**
	 * Flag that indicates whether the bean deletion is disabled
	 */
	private boolean removeDisabled;
	
	/**
	 * Flag that indicates whether the bean view is disabled  
	 */
	private boolean viewDisabled;

	public boolean isViewDisabled() {
		return viewDisabled;
	}

	public void setViewDisabled(boolean viewDisabled) {
		this.viewDisabled = viewDisabled;
	}

	public boolean isEditDisabled() {
		return editDisabled;
	}

	public void setEditDisabled(boolean editDisabled) {
		this.editDisabled = editDisabled;
	}

	public boolean isFieldsSorted() {
		return fieldsSorted;
	}

	public void setFieldsSorted(boolean fieldsSorted) {
		this.fieldsSorted = fieldsSorted;
	}

	public Class getIdType() {
		return idType;
	}

	public void setIdType(Class idType) {
		this.idType = idType;
	}

	public String getInstanceLabelExpression() {
		return instanceLabelExpression;
	}

	public void setInstanceLabelExpression(String instanceLabelExpression) {
		this.instanceLabelExpression = instanceLabelExpression;
	}

	public boolean isNewDisabled() {
		return newDisabled;
	}

	public void setNewDisabled(boolean newDisabled) {
		this.newDisabled = newDisabled;
	}

	public boolean isRemoveDisabled() {
		return removeDisabled;
	}

	public void setRemoveDisabled(boolean removeDisabled) {
		this.removeDisabled = removeDisabled;
	}

	public Class getType() {
		return type;
	}

	public void setType(Class type) {
		this.type = type;
	}

	public Collection<CrudFieldDef> getFieldDefs() {
		return fieldDefsMap.values();
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public Map<String, CrudFieldDef> getFieldDefsMap() {
		return fieldDefsMap;
	}

	public void setFieldDefsMap(Map<String, CrudFieldDef> fieldDefsMap) {
		this.fieldDefsMap = fieldDefsMap;
	}

	public CrudFieldDef getFieldDef(String fieldName) {
		return this.fieldDefsMap.get(fieldName);
	}

	/**
	 * @return the CRUDField
	 */
	public CrudFieldDef getRemoveMark() {
		for (CrudFieldDef field : this.fieldDefsMap.values()) {
			if (field.isRemoveMark()) {
				return field;
			}
		}
		return null;
	}

	/**
	 * @return Returns the browsableFields.
	 */
	public Collection<CrudFieldDef> getBrowsableFieldDefs() {
		if (this.fieldDefsMap == null) {
			throw new IllegalStateException("field definitions aren't set");
		}

		Collection<CrudFieldDef> browsableFields = new ArrayList<CrudFieldDef>();
		for (CrudFieldDef fieldDef : fieldDefsMap.values()) {
			if (fieldDef.isBrowsable()) {
				browsableFields.add(fieldDef);
			}
		}
		return browsableFields;
	}

	/**
	 * @return the browsableFields.
	 */
	public Collection<CrudFieldDef> getBrowseFilterFields() {
		if (this.fieldDefsMap == null) {
			throw new IllegalStateException("field definitions aren't set");
		}

		Collection<CrudFieldDef> browseFilterFields = new ArrayList<CrudFieldDef>();
		for (CrudFieldDef fieldDef : fieldDefsMap.values()) {
			if (fieldDef.isBrowseFilter()) {
				browseFilterFields.add(fieldDef);
			}
		}
		return browseFilterFields;
	}

	/**
	 * @return a Collection of field definitions for id fields
	 */
	public Collection<CrudFieldDef> getIds() {
		if (this.fieldDefsMap == null) {
			throw new IllegalStateException("field definitions aren't set");
		}

		Collection<CrudFieldDef> idFields = new ArrayList<CrudFieldDef>();
		for (CrudFieldDef fieldDef : fieldDefsMap.values()) {
			if (fieldDef.isId()) {
				idFields.add(fieldDef);
			}
		}
		return idFields;
	}

}
